﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using System.Text;
using System.Xml;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintAuto : System.Web.UI.Page
    {
        
        private static SqlConnection conn { get; set; }
        private static string strMessage { get; set; }
       
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPrinter();
            }
        }

        private void BindPrinter()
        {
            this.drpPrint.Items.Clear();
            ListItem defaultItem = new ListItem("请选择打印机", "-1");
            this.drpPrint.Items.Add(defaultItem);
            foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                this.drpPrint.Items.Add(iprt);
            }
            drpPrint.SelectedValue = "-1";
        }
        //开始打印
        protected void btnStart_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (drpPrint.SelectedValue == "-1")
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择打印机！");
                    return;
                }

                string strLineNum = txtLineNum.Text.Trim();
                if (string.IsNullOrEmpty(strLineNum))
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请输入检测线号！");
                    return;
                }

                if (!gfLibrary.gfValidate.Is_Integer(strLineNum))
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "检测线号必须为整数！");
                    return;
                }

                Timer1.Enabled = true;
                drpPrint.Enabled = false;
                txtLineNum.Enabled = false;
                btnStart.CssClass = "btnStartDisabled";
                btnStart.Enabled = false;
                
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + " :数据库已连接!";
                    }
                    else
                    {
                        conn.Open();
                        strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + " :数据库已经连接成功!";

                    }
                }
                else
                {
                    conn = new SqlConnection(Config.AppConfig.g_strConnectionStringSqlClient);
                    conn.Open();
                    strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + " :数据库已连接!";
                }
                lstMessage.Items.Insert(0, new ListItem() { Text = strMessage, Value = DateTime.Now.ToString() });
            }
            catch (Exception ex)
            {
                strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + " :数据库连接出错，错误信息为："+ ex.Message;
                lstMessage.Items.Insert(0, new ListItem() { Text = strMessage, Value = DateTime.Now.ToString() });
                btnStart.CssClass = "btnStart";
                btnStart.Enabled = true;
            }
        }
        //暂停打印
        protected void btnStop_Click(object sender, EventArgs e)
        {
            
            Timer1.Enabled = false;
            drpPrint.Enabled = true;
            txtLineNum.Enabled = true;
            strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + " :自动打印已停止！";
            lstMessage.Items.Insert(0, new ListItem() { Text = strMessage, Value = DateTime.Now.ToString() });
            btnStart.CssClass = "btnStart";
            btnStart.Enabled = true;
        }
        
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                this.PrintAJ();
                this.PrintZJ();
            }
            catch (Exception ex)
            {
                strMessage = DateTime.Now.ToString() + " :自动打印出错，请重新打开自动打印页面!" + ex.Message;
                lstMessage.Items.Insert(0, new ListItem() { Text = strMessage, Value = DateTime.Now.ToString() });
            }
        }
        //根据条件查询并打印报表
        private void PrintData( string Strsql,string JCLX)
        {
            int strLineNum = int.Parse(txtLineNum.Text.Trim());
            string strSql = "select top 1 * from RESULT_VEHICLE_INFO where IsPrint=0 and JCXH=" + strLineNum + " and  JCLSH like '"+ JCLX +"' and " + Strsql + "";
            SqlCommand cmd;
            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                Timer1.Enabled = false;
                return;
            }
            else            
           {             
                for (int i = 0; i < dt.Rows.Count; i++)
               {
                string strJCLSH = dt.Rows[i][4].ToString();
                string strID = dt.Rows[i][0].ToString();
                
                 if (JCLX == "A_%")
                 AutoPrintAJ(strID);
                 if (JCLX == "Z_%")
                 AutoPrintZJ(strID);
                
                strSql = "update RESULT_VEHICLE_INFO set IsPrint=1 where ID=" + strID + "";
                cmd = new SqlCommand(strSql, conn);
                cmd.ExecuteNonQuery();
                strMessage = DateTime.Now.ToString() + " :" + strJCLSH + "打印报表完成!";
                } 
              
           } 
              lstMessage.Items.Insert(0, new ListItem()
              {
                  Text = strMessage,
                  Value = DateTime.Now.ToString()
              });
             
        }

        private void AutoPrintAJ(string strKey)
        {
            ReportDocument docAJ = new ReportDocument();
            string strPathAJ = Server.MapPath("~/rpt/CrystalReportAJ.rpt");
            SetCRDataSource setData = new SetCRDataSource();
            docAJ = setData.BindAJReportEx(strKey, strPathAJ,false);
            docAJ.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docAJ.PrintToPrinter(1, false, 0, 0);

            ReportDocument docAJRG = new ReportDocument();
            string strPathRG = Server.MapPath("~/rpt/CrystalReportAJRG.rpt");
            docAJRG = setData.BindAJRGReportEx(strKey, strPathRG,false);
            docAJRG.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docAJRG.PrintToPrinter(1, false, 0, 0);

            ReportDocument docAJYQ = new ReportDocument();
            string strPathYQ = Server.MapPath("~/rpt/CrystalReportAJYQ.rpt");
            docAJYQ = setData.BindAJYQReportEx(strKey, strPathYQ,false);
            docAJYQ.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docAJYQ.PrintToPrinter(1, false, 0, 0);
        }

        private void AutoPrintZJ(string strKey)
        {
            ReportDocument docZJ = new ReportDocument();
            string strPathZJ = Server.MapPath("~/rpt/CrystalReportZJBGD.rpt");
            SetCRDataSource setData = new SetCRDataSource();
            docZJ = setData.BindZJReportEx(strKey, strPathZJ,false);
            docZJ.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docZJ.PrintToPrinter(1, false, 0, 0);

            ReportDocument docZJRG = new ReportDocument();
            string strPathRG = Server.MapPath("~/rpt/CrystalReportZJRG.rpt");
            docZJRG = setData.BindZJRGReportEx(strKey, strPathRG,false);
            docZJRG.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docZJRG.PrintToPrinter(1, false, 0, 0);

            ReportDocument docZJYQ = new ReportDocument();
            string strPathYQ = Server.MapPath("~/rpt/CrystalReportZJYQ.rpt");
            docZJYQ = setData.BindZJYQReportEx(strKey, strPathYQ,false);
            docZJYQ.PrintOptions.PrinterName = drpPrint.SelectedValue;
            docZJYQ.PrintToPrinter(1, false, 0, 0);
        }
       
        protected string ZPDSQLstr( string str)
        {
           string  AJ = "(";
           string[] zpd = PrintConfig.SelectValue(str).Split(new char[] { ',' });
           for (int i = 0; i < zpd.Length; i++)
            {
                if (i == 0 && zpd[i] != "")
                    AJ += "Z_PD=" + zpd[i] + "";
                if (i == 1 && zpd[i] != "")
                    AJ += " or Z_PD=" + zpd[i] + "";
                if (i == 2 && zpd[i] != "")
                    AJ += " or Z_PD=" + zpd[i] + "";
                if (i == 3 && zpd[i] != "")
                    AJ += " or Z_PD=" + zpd[i] + "";
            }
            AJ += ")";
            return AJ;
        }
        //安检自动打印
        protected void PrintAJ()
        {
            string AJ = "";
            if (PrintConfig.SelectValue("AJ_ZPDZT") == "1" && PrintConfig.SelectValue("AJ_SHZT") == "1")
            {
                AJ = this.ZPDSQLstr("AJ_ZPD");
                AJ += " and ";
                if (PrintConfig.SelectValue("AJ_SH") == "1")
                    AJ += " IsAudit=1 ";
                else if (PrintConfig.SelectValue("AJ_SH") == "0")
                    AJ += " IsAudit=0 ";
                PrintData(AJ, "A_%");
            }
            if (PrintConfig.SelectValue("AJ_ZPDZT") == "1" && PrintConfig.SelectValue("AJ_SHZT") == "0")
            {
                AJ = this.ZPDSQLstr("AJ_ZPD");
                PrintData(AJ, "A_%");
            }
            if (PrintConfig.SelectValue("AJ_ZPDZT") == "0" && PrintConfig.SelectValue("AJ_SHZT") == "1")
            {
                if (PrintConfig.SelectValue("AJ_SH") == "1")
                    AJ += "IsAudit=1";
                else if (PrintConfig.SelectValue("AJ_SH") == "0")
                    AJ += "IsAudit=0";
                PrintData(AJ, "A_%");
            }
            if (PrintConfig.SelectValue("AJ_ZPDZT") == "0" && PrintConfig.SelectValue("AJ_SHZT") == "0")
            {

                PrintData("0=0", "A_%");
            }
           
        }
        //综检自动打印
        protected void PrintZJ()
        {
            string ZJ = "";
            if (PrintConfig.SelectValue("ZJ_ZPDZT") == "1" && PrintConfig.SelectValue("ZJ_SHZT") == "1")
            {
                ZJ = this.ZPDSQLstr("ZJ_ZPD");
                ZJ += " and ";
                if (PrintConfig.SelectValue("ZJ_SH") == "1")
                    ZJ += " IsAudit=1 ";
                else if (PrintConfig.SelectValue("ZJ_SH") == "0")
                    ZJ += " IsAudit=0 ";
                PrintData(ZJ, "Z_%");
            }
            if (PrintConfig.SelectValue("ZJ_ZPDZT") == "1" && PrintConfig.SelectValue("ZJ_SHZT") == "0")
            {
                ZJ = this.ZPDSQLstr("ZJ_ZPD");
                PrintData(ZJ, "Z_%");
            }
            if (PrintConfig.SelectValue("ZJ_ZPDZT") == "0" && PrintConfig.SelectValue("ZJ_SHZT") == "1")
            {
                if (PrintConfig.SelectValue("ZJ_SH") == "1")
                    ZJ += "IsAudit=1";
                else if (PrintConfig.SelectValue("ZJ_SH") == "0")
                    ZJ += "IsAudit=0";
                PrintData(ZJ, "Z_%");
            }
            if (PrintConfig.SelectValue("ZJ_ZPDZT") == "0" && PrintConfig.SelectValue("ZJ_SHZT") == "0")
            {

                PrintData("0=0", "Z_%");
            }
        
        
        
        }
    
       
    }
}